package com.huishu.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huishu.commons.exception.BaseException;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.user.mapper.BuoyMapper;
import com.huishu.user.pojo.dto.BuoyDTO;
import com.huishu.user.pojo.model.BuoyDO;
import com.huishu.user.service.BuoyService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@ljj
 * @since 2023-04-22
 */
@Service
public class BuoyServiceImpl extends ServiceImpl<BuoyMapper, BuoyDO> implements BuoyService {

    @Override
    public Result handle(BuoyDTO buoyDTO, Integer type) {
        List<BuoyDO> list = new ArrayList<>();
        if (type != null) {
            if (type == 0) {
                BuoyDO buoyDO = BeanUtils.copyProperties(buoyDTO, BuoyDO.class);
                list.add(buoyDO);
                if (buoyDTO.getSort() == 1) {
                    // 首位添加
                    List<BuoyDO> pbArticleList = this.list();
                    pbArticleList.forEach(x -> {
                        x.setSort(x.getSort() + 1);
                        list.add(x);
                    });
                } else {
                    // 大于等于的都+1
                    List<BuoyDO> buoyList = this.list(Wrappers.<BuoyDO>lambdaQuery()
                            .ge(BuoyDO::getSort, buoyDTO.getSort()));
                    // 如果添加3 原来的3 4 5 都加1变成 4 5 6
                    buoyList.forEach(x -> {
                        x.setSort(x.getSort() + 1);
                        list.add(x);
                    });
                }
            } else if (type == 1) {
                // 删除时 大于自身的都减去1
                // 删除3 原来的4 5 都-1变成 3 4
                LambdaQueryWrapper<BuoyDO> queryWrapper = Wrappers.lambdaQuery(BuoyDO.class)
                        .gt(BuoyDO::getSort, buoyDTO.getSort());
                List<BuoyDO> buoyList = this.list(queryWrapper);
                buoyList.forEach(x -> {
                    if (!x.getId().equals(buoyDTO.getId())) {
                        x.setSort(x.getSort() - 1);
                        list.add(x);
                    }
                });
                this.removeById(buoyDTO.getId());
            } else {
                // 修改
                BuoyDO buoyDO = getById(buoyDTO.getId());
                if (buoyDTO.getSort().equals(buoyDO.getSort())) {
                    list.add(BeanUtils.copyProperties(buoyDTO, BuoyDO.class));
                    // 不做处理
                } else if (buoyDTO.getSort() <= buoyDO.getSort()) {
                    List<BuoyDO> buoyList = this.list(Wrappers.<BuoyDO>lambdaQuery()
                            .ge(BuoyDO::getSort, buoyDTO.getSort())
                            .lt(BuoyDO::getSort, buoyDO.getSort()));
                    buoyList.forEach(x -> {
                        x.setSort(x.getSort() + 1);
                        list.add(x);
                    });
                    list.add(BeanUtils.copyProperties(buoyDTO, BuoyDO.class));
                } else {
                    List<BuoyDO> buoyList = this.list(Wrappers.<BuoyDO>lambdaQuery()
                            .gt(BuoyDO::getSort, buoyDO.getSort())
                            .le(BuoyDO::getSort, buoyDTO.getSort()));
                    buoyList.forEach(x -> {
                        x.setSort(x.getSort() - 1);
                        list.add(x);
                    });
                    list.add(BeanUtils.copyProperties(buoyDTO, BuoyDO.class));
                }
            }
            boolean save = this.updateBatchById(list);
            return save ? Result.success() : Result.fail();
        }
        throw new BaseException("type is null");
    }
}
